package com.sky.demo.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

import com.sky.demo.R;

/**
 * @author 彬 QQ 1136096189
 * @Description: TODO drawbitmapmesh应用
 * @date 2015/8/14 15:27
 */
public class MeshView extends View {

    private int WIDTH = 200;
    private int HEIGHT = 200;
    private int COUNT = (WIDTH + 1) * (HEIGHT + 1);
    private float[] oldPoint = new float[COUNT * 2];
    private float[] newPoint = new float[COUNT * 2];
    private Bitmap mBitmap;

    public MeshView(Context context) {
        this(context, null);
    }

    public MeshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MeshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.daren_privilege_dengji);
        float bitWidth = mBitmap.getWidth();
        float bitHeight = mBitmap.getHeight();
        for (int i = 0; i < HEIGHT + 1; i++) {
            float bitY = bitHeight*i/HEIGHT;
            for (int j = 0; j < HEIGHT + 1; j++) {
                float bitX = bitWidth*j/WIDTH;
                newPoint[(i*(HEIGHT+1)+j)*2+0]=oldPoint[(i*(HEIGHT+1)+j)*2+0]=bitX;
                newPoint[(i*(HEIGHT+1)+j)*2+1]=oldPoint[(i*(HEIGHT+1)+j)*2+1]=bitY;
            }
        }
    }
    private float k=1;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < HEIGHT + 1; i++) {
            for (int j = 0; j < WIDTH + 1; j++) {
                float offSetY = (float) Math.sin((float) j / WIDTH * 2 * Math.PI+k*2* Math.PI) ;
                newPoint[(i * (HEIGHT + 1) + j) * 2 + 0] =oldPoint[(i * (HEIGHT + 1) + j) * 2 + 0]+offSetY* 100+200;
                newPoint[(i * (HEIGHT + 1) + j) * 2 + 1] = oldPoint[(i * (HEIGHT + 1) + j) * 2 + 1] + offSetY* 50 + 400;
            }
        }
        canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, newPoint, 0, null, 0, null);
        k+=0.05;
        invalidate();
    }
}
